package com.weiyi.mashangzhu.Landlord.Activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddAliAccountActivity extends AppCompatActivity {

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.account)
    EditText account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_ali_account);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.back, R.id.button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.button:
                if (name.getText() + "" == null || (name.getText() + "").equals("")) {
                    T.showShort(this, "真实姓名不能为空");
                } else {
                    if (account.getText() + "" == null || (account.getText() + "").equals("")) {
                        T.showShort(this, "支付宝账号不能为空");
                    } else {
                        LoadingDialog.show(AddAliAccountActivity.this);
                        OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "")
                                .addParams("realName", name.getText() + "")
                                .addParams("withAccount", account.getText() + "")
                                .url(API.BaseApi_L + API.AddAccount_Api_L).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(AddAliAccountActivity.this);
                                T.showShort(AddAliAccountActivity.this, "数据获取失败，请稍候再试");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(AddAliAccountActivity.this);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        finish();
                                    }
                                        T.showShort(AddAliAccountActivity.this, jsonObject.getString("msg"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }
                }
                break;
        }
    }
}
